
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
          <el-form-item label="网点名称:" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入网点名称" />
          </el-form-item>
          <el-form-item label="速邮达代码:" prop="syd_code">
            <el-input v-model="ruleForm.syd_code" placeholder="请输入速邮达代码" />
          </el-form-item>
          <el-form-item label="网点代码:" prop="code">
            <el-input v-model="ruleForm.code" placeholder="请输入网点代码" />
          </el-form-item>
          <el-form-item label="用户:" prop="user_ids">
            <el-select
              v-model="user_arr"
              multiple
              filterable
              style="width:100%"
              placeholder="请选择用户"
              @change="changeUser"
            >
              <el-option
                v-for="item in UserList"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              />
            </el-select>
            <div style="color:red;font-size:12px">
              加盟商必填，中心网点不填写可以看到全部客户
            </div>
          </el-form-item>
          <el-form-item label="网点负责人:" prop="person_charge">
            <el-input v-model="ruleForm.person_charge" placeholder="请输入网点负责人" />

          </el-form-item>
          <el-form-item label="区号:" prop="area_code">
            <el-select
              v-model="ruleForm.area_code"
              style="width:100%"
              placeholder="请选择区号"
            >
              <el-option
                v-for="item in codeList"
                :key="item.id"
                :label="'+'+item.area_code+' '+item.area_name"
                :value="item.area_code"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="电话:" prop="mobile">
            <el-input v-model="ruleForm.mobile" placeholder="请输入电话" />
          </el-form-item>
          <el-form-item label="联系地址:" prop="address">
            <el-input v-model="ruleForm.address" type="textarea" placeholder="请输入联系地址" />
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createOutlet, updateOutlet } from '@/api/PDA/index'
import { listUser } from '@/api/user/index'
import { listMobileAreaCode } from '@/api/user'

export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      time: '',
      showDialog: false,
      UserList: [],
      user_arr: [],
      codeList: [],
      ruleForm: {
        'name': '',
        'syd_code': '',
        'code': '',
        'user_ids': '',
        'person_charge': '',
        'area_code': '',
        'mobile': '',
        'address': ''
      },
      rules: {
        name: [
          { required: true, message: '请输入网点名称', trigger: 'blur' }
        ],
        syd_code: [
          { required: true, message: '请输入速邮达代码' }
        ],
        code: [
          { required: false, message: '请输入网点代码' }
        ],
        user_ids: [
          { required: false, message: '请选择用户' }
        ],
        person_charge: [
          { required: true, message: '请输入网点负责人' }
        ],
        area_code: [
          { required: true, message: '请选择区号' }
        ],
        mobile: [
          { required: true, message: '请输入电话' }
        ],
        address: [
          { required: true, message: '请输入联系地址' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        if (this.status === 2) {
          Object.keys(this.ruleForm).forEach(i => {
            this.ruleForm[i] = this.detial[i]
          })
          this.ruleForm.id = this.detial.id
          if (this.ruleForm.user_ids) {
            this.user_arr = this.ruleForm.user_ids.split(',').map(Number)
          } else {
            this.user_arr = []
          }
        }
      }
    }
  },
  mounted() {
    this.getUserList()
    this.getListMobileAreaCode()
  },
  methods: {
    getListMobileAreaCode() {
      listMobileAreaCode().then(res => {
        this.codeList = res.data.list
      })
    },
    changeUser() {
      this.ruleForm.user_ids = this.user_arr.toString()
    },
    getUserList() {
      listUser({
        limit: 100000
      }).then(res => {
        this.UserList = res.data.data
      })
    },
    close() {
      this.$emit('update', false)
      this.user_arr = []
      this.ruleForm = {
        'name': '',
        'syd_code': '',
        'code': '',
        'user_ids': '',
        'person_charge': '',
        'area_code': '',
        'mobile': '',
        'address': ''
      }
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createOutlet(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updateOutlet(this.ruleForm).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
